package com.linkturing.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import com.alibaba.fastjson.JSON;
import com.linkturing.serial.AddWeightBean;
import com.linkturing.util.Constants;
import com.linkturing.util.JedisUtil;

public class AddWeightDataTable {

	
    private String head[] = null;
    private Object [][]data = null;
    
    JButton delButton = new JButton("刪除");
	
	JButton editButton = new JButton("編輯");
	
	JButton detailButton = new JButton("详情");
	
	DefaultTableModel tableModel = null;
	
	ActionPanelEditorRenderer er = null;
	
	JFrame frame;
    
	public AddWeightDataTable(String key,JFrame frame) {
		this.frame = frame;
        //scrollPane.setBounds(0,0,700,250);
        
        
        head = new String[] {
            "序号", "起始重量", "最大重量", "加重","操作",
        };
        Object[][] dataList = queryData(key);
        JTable table = new JTable();
        
        table.setBorder(new LineBorder(new Color(0, 0, 0)));
        
		  tableModel = new DefaultTableModel(dataList,head){ 
			@Override
			public boolean isCellEditable(int row, int column) {
			  if(column==4) { return true; } 
			  return false; 
			} 
		  }; 
		  
		  table.setModel(tableModel);
		 
        table.setRowHeight(36);
		
        AddWeightSetting.scrollPane.setViewportView(table);
        AddWeightSetting.table = table;
	
	}
	
	
	public void reloadTable(String key) {
		
        //scrollPane.setBounds(0,0,700,250);
        
        JTable table = AddWeightSetting.table;
         
        table.setBorder(new LineBorder(new Color(0, 0, 0)));
        head = new String[] {
                "序号", "起始重量", "最大重量", "加重","操作",
            };
        
        Object[][] dataList = queryData(key);
        
        	tableModel = new DefaultTableModel(dataList,head){
    			private static final long serialVersionUID = 1L;
    			
    			@Override
				public boolean isCellEditable(int row, int column)
                {
    				if(column==4) {
    					return true;
    				}
                    return false;
                }
            };
        table.setModel(tableModel);
        table.setRowHeight(36);
		
		  if(dataList!=null) { 
			  if(er==null) {
				  er = new ActionPanelEditorRenderer(key); 
			  }
			  
			  TableColumn column =  table.getColumnModel().getColumn(4); 
			  column.setCellRenderer(er);
		      column.setCellEditor(er); 
		   }
		 
		 
		  AddWeightSetting.scrollPane.setViewportView(table);

	}
	
	
	
	//生成表格数据
    /**
     * @return
     */
    public Object[][] queryData(String key){
        List<AddWeightBean> list = new ArrayList<AddWeightBean>();
        
        List<String> jsonList = JedisUtil.getList(key);
        
        System.out.println("qqw:"+jsonList);
        
        if(jsonList!=null) {
        	for(int i=0;i<jsonList.size();i++) {
        		String json = jsonList.get(i);
        		AddWeightBean bean = JSON.toJavaObject(JSON.parseObject(json), AddWeightBean.class);
        		list.add(bean);
        	}
        	data = new Object[list.size()][head.length];
            for(int i=0;i<list.size();i++){
                for(int j=0;j<head.length;j++){
                    data[i][0]=String.valueOf(i+1);
                    data[i][1]=list.get(i).getStart();
                    data[i][2]=list.get(i).getEnd();
                    data[i][3]=list.get(i).getWeightIncrease();
                }
            }
        }
        
        return data;
    }
    
	class ActionPanelEditorRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {
		private static final long serialVersionUID = 1L;
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();

		public ActionPanelEditorRenderer(String key) {
			super();

			panel2.add(delButton);
			panel2.add(editButton);
//			delButton.setBackground(Color.red);

			delButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					int i = AddWeightSetting.table.getSelectedRow();
					JedisUtil.del(Constants.addweightSetting, i);

					AddWeightSetting.dataTable.reloadTable(Constants.addweightSetting);
					JOptionPane.showMessageDialog(frame, "刪除成功");
				}
			});

			editButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int i = AddWeightSetting.table.getSelectedRow();
					String start = (String) tableModel.getValueAt(i, 1);
					String end = (String) tableModel.getValueAt(i, 2);
					String weight = String.valueOf(tableModel.getValueAt(i, 3));
					AddWeightSettingEdit frame = new AddWeightSettingEdit(start, end, weight,i);
					frame.setVisible(true);
					frame.setAlwaysOnTop(true);
				}
			});
		}

		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
				int row, int column) {
			panel2.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
			panel2.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());

			// panel2.setBackground(table.getBackground());
			return panel2;
		}

		@Override
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
				int column) {
			panel2.setBackground(table.getSelectionBackground());
			return panel2;
		}

		@Override
		public Object getCellEditorValue() {
			return null;
		}
	}
}
